package com.hongning.railway.ui.main.mine.partymember;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.databinding.FragmentPartyquestionBinding;
import com.hongning.railway.entity.reqbean.QuestionResultBean;
import com.hongning.railway.entity.reqbean.ThreesessionSanDoneLessonBean;

import me.goldze.mvvmhabit.base.BaseFragment;

public class PartyQuestionFragment extends BaseFragment<FragmentPartyquestionBinding,PartyQuestionFragmentViewModel> {
    QuestionResultBean questionResultBean;
    MaterialDialog dialog;
    View dialogview;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_partyquestion;
    }
    @Override
    public PartyQuestionFragmentViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(PartyQuestionFragmentViewModel.class);
    }
    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        questionResultBean = (QuestionResultBean)getArguments().getSerializable("QuestionResultBean");
        viewModel.getCpDatas(questionResultBean);

        dialogview = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_submit,null);
        binding.appTitle.tvTopTitle.setText("测评问卷");
        binding.appTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.appTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.dialogUIChangeObserable.dialogshow.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String o) {
                if (dialog==null){
                    dialogview.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialogview.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (viewModel.status.get().equals("submit"))
                            {
                                viewModel.submit();
                            }else {
                                viewModel.save();
                            }

                            dialog.dismiss();
                        }
                    });
                    dialog = new  MaterialDialog.Builder(getActivity())
                            .customView(dialogview,false)

                            .build();
                }
                dialogview.<TextView>findViewById(R.id.tv_title).setText(o);
                dialog.show();
            }
        });
    }
}
